package com.cms.cinema.query;

import com.cms.cinema.config.QueryConfig;
import com.cms.cinema.util.FileUtils;
import lombok.extern.slf4j.Slf4j;

import java.time.Instant;
import java.util.TimerTask;

/**
 * WriterTask
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-03-24 16:46
 * @blame Team
 */
@Slf4j
public class WriterTask extends TimerTask {

    private QueryConfig queryConfig;

    public WriterTask(QueryConfig queryConfig){
        this.queryConfig = queryConfig;
    }

    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        FileUtils.fileLineWriter(Query.outVector, queryConfig.getOutputFileAbsoluteName());
        long u = (Instant.now().toEpochMilli() - Query.s)/1000;
        log.info("总计运行 {} 秒, 已完成或正在执行影院 {} 家, 已完成写入数量: {}",
                u, Query.totalCount - Query.ipVector.size(), Query.outVector.size());
    }
}
